package com.csweimei.rentmanagementtreasure.pop;

import android.content.Context;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.lxj.xpopup.core.BottomPopupView;

import java.util.ArrayList;
import java.util.List;

import me.goldze.mvvmhabit.utils.ToastUtils;

public class PopIntention extends BottomPopupView {

    private Context mContext;
    private PopSelClick mClick;
    private List<PopBean> hxList = new ArrayList<>();
    private List<PopBean> cxList = new ArrayList<>();
    private List<PopBean> otherList = new ArrayList<>();

    RecyclerView hxRecy,cxRecy,otherRecy;
    private PopBean sel;
    private String hx,cx,other;

    private TextView cancel,sure;



    public PopIntention(@NonNull Context context,  PopSelClick click) {
        super(context);
        this.mClick = click;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.pop_intention;
    }

    @Override
    protected void onCreate() {
        initData();

        hx = hxList.get(0).getName();
        cx = cxList.get(0).getName();
        other = otherList.get(0).getName();

        hxRecy = findViewById(R.id.hx_recy);
        cxRecy = findViewById(R.id.cx_recy);
        otherRecy = findViewById(R.id.other_recy);

        cancel = findViewById(R.id.cancel);
        sure = findViewById(R.id.sure);


        cancel.setOnClickListener(view ->{
            dismiss();
        });
        sure.setOnClickListener(view ->{
            String s = hx+"-"+cx+"-"+other;
            if (mClick != null){
                mClick.sel(new PopBean(s));
            }
            dismiss();

        });


        AttachAdapter hxAdapter = new AttachAdapter();
        hxRecy.setAdapter(hxAdapter);
        hxAdapter.addChildClickViewIds(R.id.tv);
        hxAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            hxAdapter.setSel(position);
            hx = hxList.get(position).getName();
        });
        hxAdapter.setList(hxList);


        AttachAdapter cxAdapter = new AttachAdapter();
        cxRecy.setAdapter(cxAdapter);
        cxAdapter.addChildClickViewIds(R.id.tv);
        cxAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            cxAdapter.setSel(position);
            cx = cxList.get(position).getName();
        });
        cxAdapter.setList(cxList);


        AttachAdapter otherAdapter = new AttachAdapter();
        otherRecy.setAdapter(otherAdapter);
        otherAdapter.addChildClickViewIds(R.id.tv);
        otherAdapter.setOnItemChildClickListener((adapter1, view, position) -> {
            otherAdapter.setSel(position);
            other = otherList.get(position).getName();

        });
        otherAdapter.setList(otherList);

    }

    private void initData() {
        hxList.add(new PopBean("1室",1));
        hxList.add(new PopBean("2室",1));
        hxList.add(new PopBean("3室",1));
        hxList.add(new PopBean("更多",1));

        cxList.add(new PopBean("朝东",1));
        cxList.add(new PopBean("朝南",1));
        cxList.add(new PopBean("朝西",1));
        cxList.add(new PopBean("朝北",1));
        cxList.add(new PopBean("东南",1));
        cxList.add(new PopBean("东北",1));
        cxList.add(new PopBean("西南",1));
        cxList.add(new PopBean("西北",1));
        cxList.add(new PopBean("暗间",1));
        cxList.add(new PopBean("不限",1));

        otherList.add(new PopBean("有电梯",1));
        otherList.add(new PopBean("无电梯",1));
        otherList.add(new PopBean("有照片",1));
        otherList.add(new PopBean("无照片",1));
        otherList.add(new PopBean("有设备",1));
        otherList.add(new PopBean("无设备",1));

    }

    private class AttachAdapter extends BaseQuickAdapter<PopBean, BaseViewHolder>{

        private int index = 0;

        public AttachAdapter() {
            super(R.layout.item_month_rent);
        }

        public void setSel(int flag){
            index = flag;
            notifyDataSetChanged();
        }

        @Override
        protected void convert(@NonNull BaseViewHolder baseViewHolder, PopBean s) {
            baseViewHolder.setText(R.id.tv,s.getName());

            if (getItemPosition(s)== index){
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_main_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.white);
            }else {
                baseViewHolder.setBackgroundResource(R.id.tv,R.drawable.rect_f6_8);
                baseViewHolder.setTextColorRes(R.id.tv,R.color.text_9d);
            }
        }
    }


}
